<?php
/**
 * 基础接口
 * @author shitou
 *
 */
class IndexAction extends CommonAction {
	/**********************界面显示*************************/
	//文件管理主界面
	public function index() {
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
		
		$this->assign('token',$upload -> createToken());
		$this->assign('FL_UPLOAD_URL',C('FL_UPLOAD_URL'));
		$this->assign('FL_STORAGE',C('FL_STORAGE'));
		$this->assign('UPLOAD_PATH',UPLOAD_PATH);
		$this->assign('FL_maxSize',C('FL_maxSize')/1024);
		
		$this->display();
	}
	
	//上传单个操作
	public function onefile() {
		$filetype = $this->_get("filetype","trim");
		$dealFun = $this->_get("dealFun","trim");
		
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
		
		$this->assign('filetype',$filetype);
		$this->assign('dealFun',$dealFun);
		$this->assign('token',$upload -> createToken());
		$this->assign('FL_UPLOAD_URL',C('FL_UPLOAD_URL'));
		$this->assign('FL_STORAGE',C('FL_STORAGE'));
		$this->assign('UPLOAD_PATH',UPLOAD_PATH);
		$this->assign('FL_maxSize',C('FL_maxSize')/1024);
		
		$this->display();
	}
	
	/**********************业务逻辑*************************/
	
	 /**
     * 文件夹列表
     * @access public
     * @return array
     */
	public function folderList(){
		$arr['addid'] = $this->_get("addid","trim");
		$arr['pid'] = $this->_get("pid","trim");
		$arr['iconSkin'] = $this->_get("iconSkin","trim");
		
		//测试变量
		$arr['addid'] = 1;
		$arr['pid'] = 0;
		$arr['iconSkin'] = 'myfolder';
		
		$folder = array();
				
		$Upload_folder = M("Upload_folder");
		$where   = "addid in (".$arr['addid'].")";
		$list = $Upload_folder->where($where)->order('addtime DESC ')->select();
		
		$i = 0;
		foreach($list as $v){
			$folder[$i]['id'] = $v['id'];
			$folder[$i]['pId'] = $v['pid'];
			$folder[$i]['name'] = $v['foldername'];
			$folder[$i]['key'] = folderFilter($v['folderkey'],true);
			$folder[$i]['iconSkin'] = $arr['iconSkin'];
			
			$i++;
		}
		
		$folder[] = array(
			'id' => '9999999',
			'pId' => '0',
			'name' => '公共文件夹',
			'key' => 'Common/',
			'open' => 0,
			'iconSkin' => $arr['iconSkin']
		);
		
		echo json_encode($folder);
	}

    /**
     * 创建文件夹
     * @access public
     * @return array
     */
	public function createFolder(){
		$rndname = randomstr(8);
		$arr['pid'] = $this->_get("pid","trim");
		$arr['foldername'] = $this->_get("foldername","urldecode");
		$arr['folderkey'] = $this->_get("pkey","trim");
		$arr['folderkey'] = $arr['folderkey'].$rndname.DIRSEP;
		$arr['addid'] = $this->_get("addid","urldecode");
		$dir = ABS_PATH.UPLOAD_PATH.$arr['folderkey'];
		$dir = folderFilter($dir);
		if(empty($arr['foldername'])){
			$arr['foldername'] = $rndname;
		}
		
		//测试变量
		$arr['addid'] = 1;
		
		$Upload_folder = M("UploadFolder");
		$Upload_folder->pid = $arr['pid'];
		$Upload_folder->foldername = $arr['foldername'];
		$Upload_folder->folderkey = $arr['folderkey'];
		$Upload_folder->addid = $arr['addid'];
		$Upload_folder->addtime = time();
		$id = $Upload_folder->add();
		
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
		$upload_status = $upload -> createFolder($dir);
		if($upload_status == 1){
			$result['status'] = 1;
			$result['info'] = '文件夹创建成功';
		}elseif($upload_status == 2){
			$result['status'] = 0;
			$result['info'] = '文件夹创建失败，原因可能是：文件夹已存在';
		}elseif($upload_status == 3){
			$result['status'] = 0;
			$result['info'] = '文件夹创建失败，原因可能是：路径不对';
		}elseif($upload_status == 4){
			$result['status'] = 0;
			$result['info'] = '文件夹创建失败，原因可能是：没有权限';
		}
		
		$result['id'] = $id;
		$result['pId'] = $arr['pid'];
		$result['foldername'] = $arr['foldername'];
		$result['path'] = $dir;
		$result['key'] = $arr['folderkey'];
		
		echo json_encode($result);
    } 
	
	/**
     * 重命名文件夹
     * @access public
     * @return array
     */
	public function renameFolder(){
		$arr['id'] = $this->_get("id","trim");
		$arr['foldername'] = $this->_get("foldername","urldecode");
		
		$Upload_folder = M("UploadFolder");
		$Upload_folder->foldername = $arr['foldername'];
		$ok = $Upload_folder->where('id='.$arr['id'])->save();
		if($ok){
			$result['status'] = 1;
			$result['info'] = '重命名成功';
		}else{
			$result['status'] = 0;
			$result['info'] = '重命名失败';
		}
		
		echo json_encode($result);
    }
	
	/**
     * 删除文件夹
     * @access public
     * @return array
     */
	public function deleteFolder(){
		function getfolderIds($pid){
			$Upload_folder = M("UploadFolder");
			$list = $Upload_folder->where('pid='.$pid)->select();
			$ids = '';
			foreach($list as $v){
				$ids = $ids.','.$v['id'];
				
				$ids .= getfolderIds($v['id']);
			}
			
			return $ids;
		}
		
		$arr['id'] = $this->_get("id","trim");
		$Upload_folder = M("UploadFolder");
		$record = $Upload_folder->where('id='.$arr['id'])->find();

		$Upload_folder = M("UploadFolder");
		$ok = $Upload_folder->where('id IN ('.$arr['id'].getfolderIds($arr['id']).')')->delete();
		
		if($ok){
			$dealClass = C('FL_STORAGE')."Upload";
			import("@.ORG.".$dealClass);
			$upload = new $dealClass();
			$upload -> deleteFolder($record['folderkey']);
			
			$result['status'] = 1;
			$result['info'] = '删除成功';
		}else{
			$result['status'] = 0;
			$result['info'] = '删除失败';
		}
		
		
		echo json_encode($result);
    }
	
	/**
     * 已知文件夹id返回文件夹key
     * @access public
     * @return array
     */
	public function getFolderKey(){
		$arr['id'] = $this->_get("folder_id","trim");
		
		$Upload_folder = M("UploadFolder");
		$record = $Upload_folder->where('id='.$arr['id'])->find();
		$arr['folderkey'] = $record['folderkey'];
		//$arr['sql'] = $Upload_folder->getLastSql();
		echo json_encode($arr);
    }
	
	/**
     * 查看文件夹内容
     * @access public
     * @return array
     */
	public function listFolder(){
		$arr['id'] = $this->_get("id","trim");
		
		$i = 0;
		$result['rows'] = array();
		
		$Upload_folder = M("UploadFolder");
		$list = $Upload_folder->where('pid='.$arr['id'])->select();
		
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
			
		foreach($list as $v){
			$result['rows'][$i]['id'] = $v['id'];
			$result['rows'][$i]['ftype'] = 'folder';
			$result['rows'][$i]['title'] = $v['foldername'];
			$result['rows'][$i]['href'] = $v['folderkey'];
			$result['rows'][$i]['thumb'] = C('FL_PLUGUN_PATH').'Images/Icon/folder.png';
			$result['rows'][$i]['tool']['see'] =1;
			$result['rows'][$i]['tool']['cut'] =0;
			$result['rows'][$i]['tool']['down'] =0;
			$result['rows'][$i]['tool']['del'] =1;
			$result['rows'][$i]['tool']['link'] =0;
			$i++;
		}
		
		$Upload_file = M("UploadFile");
		$list = $Upload_file->where('folder_id='.$arr['id'])->select();
		foreach ($list as $v) {
			//print_r($v);
			//print_r(C('FL_imageExts'));
			if(in_array($v['filetype'],C('FL_imageExts'))){
				$filethumb = explode(',',C('FL_thumbSuffix'));
				$result['rows'][$i]['test'] = $filethumb[0];
				$result['rows'][$i]['thumb'] = $upload->fileUrl($v['filekey'],$filethumb[0]);
			}else{
				$result['rows'][$i]['thumb'] = C('FL_PLUGUN_PATH').'Images/Ext/'.$v['filetype'].'.png';
			}
			
			$result['rows'][$i]['id'] = $v['id'];
			$result['rows'][$i]['ftype'] = 'file';
			$result['rows'][$i]['title'] = $v['filetitle'];
			$result['rows'][$i]['href'] = $upload->fileUrl($v['filekey'],'',false);
			$result['rows'][$i]['tool']['see'] =1;
			if($v['filetype']=='img'){
				$result['rows'][$i]['tool']['cut'] =1;
			}			
			$result['rows'][$i]['tool']['down'] =1;
			$result['rows'][$i]['tool']['del'] =1;
			$result['rows'][$i]['tool']['link'] =1;
			$i++;
		}
		
		$result['allNums'] = $i;
		
		echo json_encode($result);
    }
	
	/**
     * 上传文件
     * @access public
     * @return array
     */
	public function uploadFile(){
		//测试变量
		$arr['addid'] = 1;
		$arr['folder_id'] = $this->_post("folder_id","trim");
		$arr['title'] = $this->_post("title","unescape");
		$arr['filekey'] = $this->_post("filekey","trim");
		
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
		
		$result = $upload->uploadFile($arr);
		
		echo json_encode($result);
    }
	
	public function getLastFile(){
		$filetitle = $this->_get("filetitle","trim");
		$Upload_file = M("UploadFile");
		$ok = $Upload_file->where('filetitle="'.$filetitle.'"')->order('id DESC')->find();
		
		$result = array(
			'id'=>$ok['filekey'],
			'filetitle'=>$ok['filetitle'],
			'filekey'=>$ok['filekey'],
			'filetype'=>$ok['filekey']
		);
		
		echo json_encode($result);
    }
	
	/**
     * 重命名文件
     * @access public
     * @return array
     */
	public function renameFile(){
		$arr['id'] = $this->_get("id","trim");
		$arr['title'] = $this->_get("title","urldecode");
		
		$Upload_file = M("UploadFile");
		$Upload_file->filetitle = $arr['title'];
		$ok = $Upload_file->where('id='.$arr['id'])->save();
		if($ok){
			$result['status'] = 1;
			$result['info'] = '重命名成功';
		}else{
			$result['status'] = 0;
			$result['info'] = '重命名失败';
		}
		
		echo json_encode($result);
    }
	
	/**
     * 删除文件
     * @access public
     * @return array
     */
	public function deleteFile(){
		$arr['id'] = $this->_get("id","urldecode");
		
		$Upload_file = M("UploadFile");
		$record = $Upload_file->where('id IN ('.$arr['id'].')')->select();
		
		$dealClass = C('FL_STORAGE')."Upload";
		import("@.ORG.".$dealClass);
		$upload = new $dealClass();
		
		foreach($record as $v){
			$upload->deleteFile($v['filekey']);			
		}
		
		$Upload_file->where('id IN ('.$arr['id'].')')->delete();
		
		$result['status'] = 1;
		$result['info'] = '删除成功';
		
		echo json_encode($result);
    }
	
	/**
     * 查看文件
     * @access public
     * @return array
     */
	public function seeFile(){
		
    }
	
	/**
     * 裁剪图片
     * @access public
     * @return array
     */
	public function cropImage(){
		
    }
}
?>